package com.world.listener;

import cn.hutool.core.util.StrUtil;
import com.dtflys.forest.Forest;
import com.dtflys.forest.http.HttpStatus;
import com.world.cache.DefaultMemoryCacheOperator;
import com.world.constants.Constants;
import com.world.frame.IndexFrame;
import com.world.request.LoginClient;
import com.world.request.UserClient;
import com.world.request.dto.LoginRequest;
import com.world.request.dto.LoginResultVO;
import com.world.request.dto.ResponseData;
import com.world.request.dto.UserVO;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录按钮监听
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/11/28 16:23
 */
public class LoginButtonListener implements ActionListener {

    private final JFrame frame;
    private final JTextField userNameField;
    private final JPasswordField passwordField;

    public LoginButtonListener(JFrame frame, JTextField userNameField, JPasswordField passwordField) {
        this.frame = frame;
        this.userNameField = userNameField;
        this.passwordField = passwordField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String text = userNameField.getText();
        if (text.trim().equals("用户名") || StrUtil.isBlank(text)) {
            JOptionPane.showMessageDialog(null, "请输入登录名称！！");
            return;
        }

        char[] password = passwordField.getPassword();
        String passwordStr = new String(password);
        if (password.length <= 0) {
            JOptionPane.showMessageDialog(null, "请输入密码！！");
            return;
        }
        if (passwordStr.trim().equals("密码")) {
            JOptionPane.showMessageDialog(null, "请输入密码！！");
            return;
        }

        LoginClient client = Forest.client(LoginClient.class);
        LoginRequest request = new LoginRequest();
        request.setUserName(text);
        request.setPassword(passwordStr);
        ResponseData<LoginResultVO> result = client.login(request);
        if (result.getCode().equals(HttpStatus.OK)) {
            //保存token并且获取用户信息
            if (saveTokenAndGetUserInfo(result)) {
                frame.dispose();
                new IndexFrame();
            }
        } else {
            JOptionPane.showMessageDialog(null, "账号或密码错误！！");
        }
    }

    /**
     * 保存token并且获取用户信息
     *
     * @param data ResponseData
     * @author WangYX
     * @date 2023/11/29 10:29
     */
    public boolean saveTokenAndGetUserInfo(ResponseData<LoginResultVO> data) {
        LoginResultVO vo = data.getData();
        DefaultMemoryCacheOperator.put(Constants.TOKEN, vo.getToken());

        UserClient client = Forest.client(UserClient.class);
        ResponseData<UserVO> result = client.getCurrentUser(DefaultMemoryCacheOperator.get(Constants.TOKEN).toString());
        if (result.getCode().equals(HttpStatus.OK)) {
            DefaultMemoryCacheOperator.put(Constants.USER_KEY, result.getData());
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "获取用户信息失败！！");
            return false;
        }
    }


}
